package com.dao.impl;

import com.dao.BookDao;
import com.dao.DButil;
import com.model.Book;
import com.model.BookView;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BookDaoimpl extends DButil implements BookDao {
    public List<BookView> queryEmps(int bookStatus) {
        String sql = "select book.id,book.b_name,book.b_price," +
                "book.upload_time,book.borrow_id,book.borrow_time," +
                "book.state" +
                " from book book " +
                "where book.state=? or 1=1";
        return super.executeQueryList(sql, BookView.class, bookStatus);
    }

    @Override
    public void addBook(Book book) {
        System.out.println("book = " + book);
//        -- insert into book values(default,b_name,b_price,upload_time,borrow_id,borrow_time,state);
        String sql = "insert into book values(default,?,?,?,?,?,?)";
        super.executeUpdate(sql,book.getB_name(),book.getB_price(),book.getUpload_time(),book.getBorrow_id(),
                book.getBorrow_time(),book.getState());
//        CallableStatement callableStatement=null;
//            String calSql="{call proc_add_book(?,?,?,?,?,?}";
//            try {
//                Connection conn = super.getConn();
//                //id name price upload_time borrow_id borrow_time state
//                callableStatement = conn.prepareCall(calSql);
//                callableStatement.setString(1,book.getB_name());
//                callableStatement.setString(2, book.getB_price());
//                callableStatement.setString(3,  book.getUpload_time());
//                callableStatement.setString(4,  book.getBorrow_id());
//                callableStatement.setString(5,  book.getBorrow_time());
//                callableStatement.setInt(6, book.getState());
//                System.out.println("执行成功");
//                callableStatement.executeUpdate();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
    }
    public void updateEmp(Book book) {
        String sql = "update book set book.name=?,book.price=?,book.upload_time=?,book.borrow_id=?,book.borrow_time=?,book.state=? where book.id=?";
        super.executeUpdate(sql, book.getB_name(), book.getB_price(), book.getUpload_time(), book.getBorrow_id(), book.getBorrow_time(), book.getState(),book.getId());
    }

}
